using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Sio.Mdm.Utils
{
    /// <summary>
    /// Class for parsing string values to strong typed values
    /// </summary>
    public static class StringParser
    {
        #region Methods

        #region Public

        #region GetInteger
        /// <summary>
        /// Get integer value from string
        /// </summary>
        /// <param name="value">String value to convert to integer</param>
        /// <returns>Integer value of string or <see cref="Int32.MinValue"/> when error occured</returns>
        public static int GetInteger(string value)
        {
            return GetInteger(value, Int32.MinValue);
        }
        /// <summary>
        /// Get integer value from string or return default value if parsing fails
        /// </summary>
        /// <param name="value">String value to convert to integer</param>
        /// <param name="defaultValue">Default value to return in case of error</param>
        /// <returns>Parsed integer value or <see cref="Int32.MinValue"/></returns>
        public static int GetInteger(string value, int defaultValue)
        {
            int result;

            if (!Int32.TryParse(value, out result))
            {
                result = defaultValue;
            }

            return result;
        }
        #endregion  

        #region GetDouble
        /// <summary>
        /// Get integer value from string
        /// </summary>
        /// <param name="value">String value to convert to integer</param>
        /// <returns>Integer value of string or <see cref="Double.NaN"/> when error occured</returns>
        public static double GetDouble(string value)
        {
            return GetDouble(value, Double.NaN);
        }
        /// <summary>
        /// Get double value from string or return default value if parsing fails
        /// </summary>
        /// <param name="value">String value to convert to integer</param>
        /// <param name="defaultValue">Default value to return in case of error</param>
        /// <returns>Parsed double value or <see cref="Double.NaN"/></returns>
        public static double GetDouble(string value, double defaultValue)
        {
            double result;

            if (!Double.TryParse(value, out result))
            {
                result = defaultValue;
            }

            return result;
        }
        #endregion  

        #region GetGuid
        /// <summary>
        /// Parse guid from string
        /// </summary>
        /// <param name="value">String value to parse</param>
        /// <returns>Guid from parsed string or <see cref="Guid.Empty"/> if value cannot be parsed</returns>
        public static Guid GetGuid(string value)
        {
            return GetGuid(value, Guid.Empty);
        }
        /// <summary>
        /// Parse guid from string
        /// </summary>
        /// <param name="value">String to parse</param>
        /// <param name="defaultValue">Default value to return on failure</param>
        /// <returns>Parsed guid valu of <see cref="Guid.Empty"/> when parsing fails</returns>
        public static Guid GetGuid(string value, Guid defaultValue)
        {
            Guid result;

            try
            {   
                result = new Guid(value);
            }
            catch
            {
                result = defaultValue;
            }

            return result;
        }
        #endregion

        #region GetDateTime
        /// <summary>
        /// Parse date and time from string
        /// </summary>
        /// <param name="value">String value to parse</param>
        /// <returns>Guid from parsed string or <paramref name="defaultValue"/> if value cannot be parsed</returns>
        public static DateTime GetDateTime(string value)
        {
            return GetDateTime(value, DateTime.MinValue);
        }
        /// <summary>
        /// Parse date and time from string
        /// </summary>
        /// <param name="value">String to parse</param>
        /// <param name="defaultValue">Default value to return on failure</param>
        /// <returns>DateTime from parsed string or <paramref name="defaultValue"/> if value cannot be parsed</returns>
        public static DateTime GetDateTime(string value, DateTime defaultValue)
        {
            DateTime result;

            if (!DateTime.TryParse(value, out result))
            {
                result = defaultValue;
            }
            
            return result;
        }
        #endregion

        #region GetBoolean
        /// <summary>
        /// Parse boolean from string
        /// </summary>
        /// <param name="value">String value to parse</param>
        /// <returns>Guid from parsed string or false if value cannot be parsed</returns>
        public static bool GetBoolean(string value)
        {
            return GetBoolean(value, false);
        }
        /// <summary>
        /// Parse boolean from string
        /// </summary>
        /// <param name="value">String to parse</param>
        /// <param name="defaultValue">Default value to return on failure</param>
        /// <returns>Boolean from parsed string or <paramref name="defaultValue"/> if value cannot be parsed</returns>
        public static bool GetBoolean(string value, bool defaultValue)
        {
            bool result;

            if (!Boolean.TryParse(value, out result))
            {
                result = defaultValue;
            }

            return result;
        }
        #endregion

        #region Camelize
        /// <summary>
        /// Return camelized string
        /// </summary>
        /// <param name="text">Text to camelize</param>
        /// <returns></returns>
        public static string Camelize(string text)
        {
            text = text.ToLower();

            if (text.EndsWith("id"))
            {
                text = text.Replace("id", String.Empty);
                return String.Format("{0}{1}Id", Char.ToUpper(text[0]), text.Substring(1));
            }

            return String.Format("{0}{1}", Char.ToUpper(text[0]), text.Substring(1));
        }
        #endregion

        #endregion

        #endregion
    }
}
